package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.Dto.SetmealDto;
import com.itheima.common.R;
import com.itheima.pojo.*;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Stack;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;


    //添加
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {

        setmealService.saveWithDish(setmealDto);

        return R.success("添加成功！");
    }


    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //创建page
        Page<Setmeal> page1 = new Page<>(page, pageSize);
        // 创建Dto的page
        Page<SetmealDto> dtoPage = new Page<>();
        //创建条件构造器
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //模糊查询 如果名字不为空  就去找setmal表里的name。
        wrapper.like(name != null, Setmeal::getName, name);
        //根据修改时间进行排序
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(page1, wrapper);


        //拷贝对象
        BeanUtils.copyProperties(page1, dtoPage, "records");
        // 获取到页面展示的数据
        List<Setmeal> records = page1.getRecords();
        //遍历records
        List<SetmealDto> list = records.stream().map((item) -> {
            //新建一个dto对象
            SetmealDto setmealDto = new SetmealDto();
            //拷贝参数和对象
            BeanUtils.copyProperties(item, setmealDto);
            //参数获取口味表的id
            Long id = item.getCategoryId();
            //分类方法获取id
            Category byId = categoryService.getById(id);
            //如何id不为空
            if (byId != null) {
                //用id获取到口味的名字
                String name1 = byId.getName();
                //把口味名字放到dto里
                setmealDto.setCategoryName(name1);
            }
            //返回一个dto
            return setmealDto;
        }).collect(Collectors.toList());
        //把通过口味表集合获取的名字放到dto里
        dtoPage.setRecords(list);
        //整个数据里
        return R.success(dtoPage);

    }

    //删除
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealService.deletedWithDish(ids);
        return R.success("套餐数据删除成功");
    }


    //停售
    @PostMapping("/status/{status}")

    public R<String> status(@PathVariable Integer status, Long[] ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);

        List<Setmeal> list = setmealService.list(wrapper);
        int s = status == 1 ? 1 : 0;
        for (Setmeal setmeal1 : list) {
            setmeal1.setStatus(s);
            setmealService.updateById(setmeal1);
        }
        return R.success("操作成功");
    }


    //回显数据
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id) {
        SetmealDto byIdWithDish = setmealService.getByIdWithDish(id);

        return R.success(byIdWithDish);

    }

    //修改数据
    @PutMapping
    public R<String> updateSetMeal(@RequestBody SetmealDto setmealDto) {
        //更新setmeal
        setmealService.updateById(setmealDto);
        //更新setmeal对应的菜品，先删除再添加
        Long Id = setmealDto.getId();

        List<SetmealDish> list = setmealDto.getSetmealDishes();
        for (SetmealDish dish : list) {
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId, Id);
            setmealDishService.remove(wrapper);

            dish.setSetmealId(Id);
        }
        setmealDishService.saveBatch(list);
        return R.success("修改成功！");
    }

    //手机端根据套餐分类id查询该套餐分类下的套餐
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getCreateTime);
        List<Setmeal> list = setmealService.list(wrapper);
        return R.success(list);
    }

}
